{
 "cells": [
  {
   "cell_type": "markdown",
   "source": [
    "Chapter 08\n",
    "# 自定义函数中使用**kwargs\n",
    "Book_1《编程不难》 | 鸢尾花书：从加减乘除到机器学习"
   ],
   "metadata": {
    "collapsed": false
   }
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "outputs": [],
   "source": [
    "# 利用*kwargs\n",
    "def multiply_all_2(**kwargs):\n",
    "    result = 1\n",
    "    print(type(kwargs))\n",
    "    # 循环dict()\n",
    "    for key, value in kwargs.items():\n",
    "        print(\"The value of {} is {}\".format(key, value))\n",
    "        result *= value\n",
    "\n",
    "    return result"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:31:52.790636Z",
     "end_time": "2024-07-03T16:31:52.794149Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "The value of A is 1\n",
      "The value of B is 2\n",
      "The value of C is 3\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "# 计算3个key-value pairs中值的乘积\n",
    "print(multiply_all_2(A=1, B=2, C=3))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:32:02.892327Z",
     "end_time": "2024-07-03T16:32:02.900807Z"
    }
   }
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "<class 'dict'>\n",
      "The value of A is 1\n",
      "The value of B is 2\n",
      "The value of C is 3\n",
      "The value of D is 4\n",
      "24\n"
     ]
    }
   ],
   "source": [
    "# 计算4个key-value pairs中值的乘积\n",
    "print(multiply_all_2(A=1, B=2, C=3, D=4))"
   ],
   "metadata": {
    "collapsed": false,
    "ExecuteTime": {
     "start_time": "2024-07-03T16:32:21.853213Z",
     "end_time": "2024-07-03T16:32:21.873409Z"
    }
   }
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 2
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython2",
   "version": "2.7.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
